package com.orangelife.mobile.myhouse.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.VolleyHttpUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.common.jump.HouseJumper;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.myhouse.biz.HouseBiz;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 住户详细信息
 * <p/>
 * Created by 吴格非 on 2015/11/13.
 */
public class ResidentDetailActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.tvIdentity)
    TextView tvIdentity;
    @Bind(R.id.tvName)
    TextView tvName;
    @Bind(R.id.tvPhone)
    TextView tvPhone;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    private String applicationID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resident_detail);
        ButterKnife.bind(this);
        getIntentInfo();
        setListener();
    }

    private void getIntentInfo() {
        ArrayList<String> list = getIntent().getStringArrayListExtra(HouseJumper.KEY_RESIDENT);
        if (list != null) {/*从房屋列表过来的（非产权人直接跳详情），需要重新请求数据*/
            applicationID = list.get(0);
            requestResidentDetail();
        } else {/*从用户列表直接过来的，带数据*/
            String name = getIntent().getStringExtra("resident_list_name");
            String phone = getIntent().getStringExtra("resident_list_phone");
            String identity = getIntent().getStringExtra("resident_list_identity");
            initView(name, phone, identity);
            emptyLayout.dismiss();
        }
        initTitle();
    }

    private void initView(String name, String phone, String identity) {
        tvName.setText(name);
        tvPhone.setText(phone);
        tvIdentity.setText(HouseBiz.getIdentityName(this, identity));
    }

    private void setListener() {
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestResidentDetail();
            }
        });
    }

    private void requestResidentDetail() {
        HouseHttpBiz.getResidentDetail(applicationID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                emptyLayout.dismiss();
                Map<String, Object> map = HouseHttpBiz.handleResidentDetail(response);
                if (map != null) {
                    String phone = String.valueOf(map.get("phone"));
                    String identity = String.valueOf(map.get("type"));
                    String name = String.valueOf(map.get("name"));
                    initView(name, phone, identity);
                } else {
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                }
            }
        });
    }

    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;

    private void initTitle() {
        tvTitle.setText("身份认证");
    }
}
